package danny.hoi.service.impl;

import danny.hoi.dao.CategoryDao;
import danny.hoi.pojo.TbCategory;
import danny.hoi.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    private final static String REDIS_CATEGORY_LIST = "REDIS_CATEGORY_LIST";
    private final static String REDIS_CATEGORY_ID = "REDIS_CATEGORY_ID";

    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void add(TbCategory category) {
        //一级分类
        if (category.getParentId() == null) {
            category.setParentId(0);
        }
        //二级分类
        if (category.getCategoryList() != null && category.getCategoryList().size() > 0) {
            for (TbCategory tbCategory : category.getCategoryList()) {
                //获取父类id
                tbCategory.setParentId(category.getParentId());
                categoryDao.insertSelective(tbCategory);
            }
        }

        //删除redis中的数据
        try {
            redisTemplate.delete(REDIS_CATEGORY_LIST);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void update(TbCategory category) {


        categoryDao.updateByPrimaryKeySelective(category);
        TbCategory tbCategory = new TbCategory();


        tbCategory.setParentId(category.getId());
        List<TbCategory> tbCategories = categoryDao.select(tbCategory);
        categoryDao.delete(tbCategory);

        if (tbCategories != null && tbCategories.size() > 0) {
            for (TbCategory tbc : tbCategories) {
                deleteRedisId(tbc.getId());
            }
        }

        //重新添加
        if (category.getCategoryList() != null && category.getCategoryList().size() > 0) {
            for (TbCategory cate : category.getCategoryList()) {
                cate.setParentId(category.getId());
            }
            categoryDao.insertList(category.getCategoryList());
        }

        deleteRedisId(category.getId());
    }


    @Override
    public TbCategory findOne(Integer id) {
        TbCategory tbCategory = null;

        try {
            tbCategory = (TbCategory) redisTemplate.boundHashOps(REDIS_CATEGORY_ID).get(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (tbCategory == null) {
            tbCategory = categoryDao.selectByPrimaryKey(id);
            TbCategory category = new TbCategory();
            category.setParentId(tbCategory.getId());
            List<TbCategory> categoryList = categoryDao.select(category);

            tbCategory.setCategoryList(categoryList);


            try {
                redisTemplate.boundHashOps(REDIS_CATEGORY_ID).put(id, tbCategory);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        return tbCategory;
    }


    @Override
    public List<TbCategory> findAll() {
        List<TbCategory> categoryList = null;

        try {
            categoryList = (List<TbCategory>) redisTemplate.boundValueOps(REDIS_CATEGORY_LIST).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (categoryList == null || categoryList.size() == 0) {
            TbCategory tbCategory = new TbCategory();
            Example example = new Example(TbCategory.class);
            example.orderBy("parentId").asc();
            example.createCriteria().andEqualTo("parentId", 0);
            categoryList = categoryDao.selectByExample(example);
            //查找子分类
            for (TbCategory category : categoryList) {
                tbCategory.setParentId(category.getId());
                category.setCategoryList(categoryDao.select(tbCategory));
            }

            try {
                redisTemplate.boundValueOps(REDIS_CATEGORY_LIST).set(categoryList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return categoryList;
    }


    @Override
    public TbCategory findCategoryById(Integer id) {

        TbCategory category = findOne(id);

        TbCategory tbCategory = null;

        try {
            tbCategory = (TbCategory) redisTemplate.boundHashOps(REDIS_CATEGORY_ID).get(category.getParentId());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (tbCategory == null) {
            tbCategory = categoryDao.selectByPrimaryKey(category.getParentId());
        }

        return tbCategory;
    }

    @Override
    public void deleteList(Integer[] ids) {
        for (Integer id : ids) {
            deleteRedisId(id);
        }

        categoryDao.deleteByIds(StringUtils.join(ids, ","));

        //删除子分类
        TbCategory tbCategory = new TbCategory();
        for (Integer id : ids) {
            tbCategory.setParentId(id);
            List<TbCategory> list = categoryDao.select(tbCategory);
            categoryDao.delete(tbCategory);

            if (list != null && list.size() > 0) {
                for (TbCategory category : list) {
                    deleteRedisId(category.getId());
                }
            }
        }
    }

    private void deleteRedisId(Integer id) {
        try {
            redisTemplate.boundHashOps(REDIS_CATEGORY_ID).delete(id);
            redisTemplate.delete(REDIS_CATEGORY_LIST);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
